﻿using Jw.Core;
using Jw.Models;
using Jw.Web.Areas.Dev.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

namespace Jw.Web.Areas.Dev.Controllers
{
    public class SbrhbwController : Controller
    {
        /// <summary>
        /// 设备润滑控制器
        /// </summary>
        public ActionResult Index()
        {
            var codeService = new sys_codeService();
            var model = new
            {
                idField = "Sbrhxm_Xmbh",
                urls = DevHelper.GetIndexUrls("Sbrhbw"),
                resx = DevHelper.GetIndexResx("设备润滑项目"),
                dataSource = new
                {
                },
                form = new
                {
                    Sbrhxm_Xmbh = ""
                },
                defaultRow = new
                {
                },
                setting = new
                {
                    postListFields = new string[] { "Sbrhxm_Xmbh", "Sbrhxm_Xmmc", "Sbrhxm_Lbbh", "Sbrhxm_Sblb", "Sbrhxm_Jtyq", "Sbrhxm_Fzr", "Sbrhxm_Remark" }
                }
            };
            return View(model);
        }

    }


    public class SbrhbwApiController : ApiController
    {
        /// <summary>
        /// 设备润滑-获取设备的最新编号
        /// </summary>
        /// <returns></returns>
        public string getnewbillno()
        {
            return new b_SbrhxmService().GetNewKey("Sbrhxm_Xmbh", "dateplus");
        }
        /// <summary>
        /// 设备润滑部位编号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string getnewcode(string id)
        {
            return new b_SbrhxmService().GetNewCode("Sbrhxm_Xmbh", "Sbrhxm_Lbbh", id);
        }
        /// <summary>
        /// 设备润滑-获取
        /// </summary>
        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='Sbrhxm_Xmbh DESC'>
            <select>*</select>
                <from>b_Sbrhxm</from>
                    <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                        <field name='Sbrhxm_Xmbh' cp='like'></field>
                    </where>
            </settings>");
            var service = new b_SbrhxmService();
            var pQuery = query.ToParamQuery();
            var result = service.GetDynamicListWithPaging(pQuery);

            return result;
        }
        /// <summary>
        /// 设备润滑-编辑
        /// </summary>
        [System.Web.Http.HttpPost]
        public void Edit(dynamic data)
        {
            var formWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='Sbrhxm_Xmbh' cp='equal'></field></where>
            </settings>", typeof(b_Sbrhxm).Name);

            var result = new b_SbrhxmService().Edit(null, formWrapper, data);
        }
        /// <summary>
        /// 设备润滑- 删除（删除主表前是删除明细)
        /// </summary>
        public void Delete(string id)
        {
            var result = new b_SbrhxmService().Delete(ParamDelete.Instance().AndWhere("Sbrhxm_Xmbh", id));
            DevHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Sbda_Sbbm={0}]，请重试或联系管理员！", id);
        }

        #region 设备类别
        /// <summary>
        /// 设备润滑类别-查询 
        /// </summary>
        public dynamic GetTypes(RequestWrapper request)
        {
            request.LoadSettingXmlString(@"
            <settings defaultOrderBy='Sbrhlb_Lbbh'>
               <where defaultIgnoreEmpty='true'>
                    <field name='Sbrhlb_Lbbh'  cp='equal'></field>
                    <field name='Sbrhlb_Lbmc'  cp='like' ></field>
                </where>
            </settings>
            ");
            var result = new b_SbrhlbService().GetDynamicList(request.ToParamQuery());
            return result;
        }
        /// <summary>
        /// 设备润滑类别-编辑
        /// </summary>
        [System.Web.Http.HttpPost]
        public void EditType(dynamic data)
        {
            var listWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>b_Sbrhlb</table>
                <where>
                    <field name='Sbrhlb_Lbbh' cp='equal'></field>
                </where>
            </settings>");
            var service = new b_SbrhlbService();
            var result = service.Edit(null, listWrapper, data);
        }
        #endregion
    }
}
